using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Model.Attendance;
using EzDesk.Apps.ServiceImpl.Attendance.Repositories.Interface;

namespace EzDesk.Apps.ServiceImpl.Attendance.Repositories
{
    public partial class ModifiedEvaluationRepository : IModifiedEvaluationRepository
    {
        EzDeskAttendanceDataContext _db;

        public ModifiedEvaluationRepository(EzDeskAttendanceDataContext db)
        {
            this._db = db;
        }
		
		#region IRepository<ModifiedEvaluation> Members

        public Guid Add(DayAttendanceEvaluation model, bool saveChanges)
        {   
            _db.ez_ModifiedEvaluations.InsertOnSubmit(
                new ez_ModifiedEvaluation()
                {
					EvaluationId = model.Id,
								
		            UserId = model.UserId,
					Date = model.Date,						
					LateMiniutes = model.LateMiniutes,						
					EarlyMiniutes = model.EarlyMiniutes,						
					IsAbsense = model.IsAbsense,						
					IsExtraWork = model.IsExtraWork,						
					ManHours = model.Manhours,						
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public DayAttendanceEvaluation GetById(Guid id)
        {
            DayAttendanceEvaluation info = null;
            if (id != null)
            {
                info = _db.ez_ModifiedEvaluations.Where(p => p.EvaluationId == id)
                    .Select(p => new DayAttendanceEvaluation()
                    {
						Id = p.EvaluationId,
				        		
	                    UserId = p.UserId,
						Date = p.Date,						
						LateMiniutes = p.LateMiniutes,						
						EarlyMiniutes = p.EarlyMiniutes,						
						IsAbsense = p.IsAbsense,						
						IsExtraWork = p.IsExtraWork,						
						Manhours = p.ManHours,						
                    })
                    .SingleOrDefault();
            }
            return info;
        }

        public IQueryable<DayAttendanceEvaluation> GetAll()
        {
            return _db.ez_ModifiedEvaluations.Select(
                p => new DayAttendanceEvaluation()
                {
					Id = p.EvaluationId,
							
			        UserId = p.UserId,
					Date = p.Date,						
					LateMiniutes = p.LateMiniutes,						
					EarlyMiniutes = p.EarlyMiniutes,						
					IsAbsense = p.IsAbsense,						
					IsExtraWork = p.IsExtraWork,						
					Manhours = p.ManHours,						
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_ModifiedEvaluation entity = _db.ez_ModifiedEvaluations.Where(p => p.EvaluationId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_ModifiedEvaluations.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(DayAttendanceEvaluation model, bool saveChanges)
        {
            var entity = _db.ez_ModifiedEvaluations.Where(p => p.EvaluationId == model.Id)
                .SingleOrDefault();

            entity.UserId = model.UserId;
			entity.Date = model.Date;
			entity.LateMiniutes = model.LateMiniutes;
			entity.EarlyMiniutes = model.EarlyMiniutes;
			entity.IsAbsense = model.IsAbsense;
			entity.IsExtraWork = model.IsExtraWork;
			entity.ManHours = model.Manhours;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
		#endregion
    }
}
